<?php

namespace App\Http\Services;

use App\Utils\RSA;

class RSAService
{
    private const TZ_PRI_KEY_FILE = 'zhengshu/tz_rsa_private_key.pem';  // 同铸私钥文件
    private const TZ_PUB_KEY_FILE = 'zhengshu/tz_rsa_public_key.pem';   // 同铸公钥文件
    private const SIP_PUB_KEY_FILE= 'zhengshu/sip_rsa_public_key.pem';  // sip公钥文件

    /**
     * 同铸私钥
     * @return string
     */
    private static function tzPriKeyFile() : string {
        return base_path(self::TZ_PRI_KEY_FILE);
    }

    /**
     * 同铸公钥
     * @return string
     */
    private static function tzPubKeyFile() : string {
        return base_path(self::TZ_PUB_KEY_FILE);
    }

    /**
     * SIP公钥
     * @return string
     */
    private static function sipPubKeyFile() : string{
        return base_path(self::SIP_PUB_KEY_FILE);
    }

    /**
     * 同铸私钥加密
     * @param $data
     * @return mixed
     */
    static function tz_private_encrypt($data) {
        $privateKeyFile = self::tzPriKeyFile();
        $rsa = new RSA();
        $rsa->setPrivKey($privateKeyFile);
        $encrypted = $rsa->privEncrypt($data);
        return $encrypted;
    }

    /**
     * 同铸私钥解密
     * @param $data
     * @return mixed
     */
    static function tz_private_decrypt($data) {
        $priKeyFile = self::tzPriKeyFile();
        $rsa = new RSA();
        $rsa->setPrivKey($priKeyFile);
        return $rsa->privDecrypt($data);
    }

    /**
     * 同铸公钥加密
     * @param $data
     * @return mixed
     */
    static function tz_public_encrypt($data) {
        $pubKeyFile = self::tzPubKeyFile();
        $rsa        = new RSA();
        $rsa->setPubKey($pubKeyFile);
        return $rsa->pubEncrypt($data);
    }

    /**
     * 公钥解密
     * @param $data
     * @return mixed
     */
    static function tz_public_decrypt($data) {
        $pubKeyFile = self::tzPubKeyFile();
        $rsa        = new RSA();
        $rsa->setPubKey($pubKeyFile);
        return $rsa->pubDecrypt($data);
    }

    /**
     * sip加密
     * @param $data
     * @return mixed
     */
    static function sip_public_encrypt($data) {
        $pubKeyFile = self::sipPubKeyFile();
        $rsa = new RSA();
        $rsa->setPubKey($pubKeyFile);
        return $rsa->pubEncrypt($data);
    }

    /**
     * sip解密
     * @param $data
     * @return mixed
     */
    static function sip_public_decrypt($data) {
        $pubKeyFile = self::sipPubKeyFile();
        $rsa = new RSA();
        $rsa->setPubKey($pubKeyFile);
        return $rsa->pubDecrypt($data);
    }

    /**
     * 同铸私钥加签
     */
    static function tz_sign($data) {
        $priKeyFile = self::tzPriKeyFile();
        $rsa = new RSA();
        $rsa->setPrivKey($priKeyFile);
        return $rsa->priSign($data);
    }

    /**
     * 同铸公钥解签
     * @param $data
     * @param $sign
     * @return bool|string|null
     */
    static function tz_verify($data, $sign) {
        $pubKeyFile = self::tzPubKeyFile();
        $rsa = new RSA();
        $rsa->setPubKey($pubKeyFile);
        return $rsa->pubVerify($data, $sign);
    }

    /**
     * sip公钥延签
     */
    static function sip_verify($data, $sign) {
        $pubKeyFile = self::sipPubKeyFile();
        $rsa = new RSA();
        $rsa->setPubKey($pubKeyFile);
        return $rsa->pubVerify($data, $sign);
    }
}
